<?php
session_start();
$order_user = $_SESSION['userid'];
date_default_timezone_set('prc');

$tableOrder = "orderlist";

$admin_year = $_REQUEST['year'];
$admin_month = $_REQUEST['month'];
$admin_days = $_REQUEST['days'];
$admin_ats = $_REQUEST['ats'];
$admin_dapartment = $_REQUEST['depart'];


//传过来的名字是?
$selectName = $_REQUEST['selectName'];



if(($selectName)&&($selectName!=all))
{
    $search_name_str="and user_name='".$selectName."'";
    //echo "<br/>指定人名!";
}




//不指定日，不指定餐
if($admin_days==0&&$admin_ats==0&&$admin_dapartment!=''){
    $search_key = $admin_year.$admin_month;

    //echo "//不指定日，不指定餐";


    //不指定部门
    if($admin_dapartment=='all'){
        //echo "不指定部门";
        $reStr = $admin_year."年".$admin_month."月";
        $query = mysql_query("select count(nid) from $tableOrder where search_key='$search_key'".$search_name_str);
        $result = mysql_query("select * from  $tableOrder where search_key='$search_key'".$search_name_str." order by order_date,user_id");
    }


    else
    {//指定部门
        //echo "//指定部门";
        $reStr = $admin_year."年".$admin_month."月 ".$admin_dapartment;
        $query = mysql_query(
            "select count(nid) from $tableOrder where search_key='$search_key' and user_department='$admin_dapartment'".$search_name_str);

        $result = mysql_query(
            "select * from $tableOrder where search_key='$search_key' and user_department='$admin_dapartment' ".$search_name_str." order by  order_date,user_id");
    }




    $sum = mysql_result($query, 0);
    while ($row = mysql_fetch_array($result)) {
        $search_name[] = $row['product_name'];
        $search_id[] = $row['product_id'];
        $search_date[] = $row['order_date'];
        $search_num[] = $row['product_num'];
        $search_at[] = $row['order_at'];
        $search_user[] = $row['user_id'];
        $search_user_name[] = $row['user_name'];
        $search_one_price[] = $row['product_price'];
        $search_price[] = $row['order_price'];
        $search_diliver[] = $row['diliver'];
    }

}


//定日，不指定餐
else if($admin_days!==0&&$admin_ats==0){

    $searchStr = $admin_year."年".$admin_month."月".$admin_days."日";
    if($admin_dapartment=='all'){
        $reStr = $admin_year."年".$admin_month."月".$admin_days."日";
        $query = mysql_query("select count(nid) from $tableOrder where order_date='$searchStr' ".$search_name_str);
        $result = mysql_query("select * from $tableOrder where order_date='$searchStr' ".$search_name_str."order by nid");
    }else


    {
        $reStr = $admin_year."年".$admin_month."月".$admin_days."日 ".$admin_dapartment;
        $query = mysql_query("select count(nid) from $tableOrder where order_date='$searchStr' and user_department='$admin_dapartment'".$search_name_str);
        $result = mysql_query("select * from $tableOrder where order_date='$searchStr' and user_department='$admin_dapartment'".$search_name_str." order by nid");
    }


    $sum = mysql_result($query, 0);


    while ($row = mysql_fetch_array($result)) {
        $search_name[] = $row['product_name'];
        $search_id[] = $row['product_id'];
        $search_date[] = $row['order_date'];
        $search_num[] = $row['product_num'];
        $search_at[] = $row['order_at'];
        $search_user[] = $row['user_id'];
        $search_user_name[] = $row['user_name'];
        $search_one_price[] = $row['product_price'];
        $search_price[] = $row['order_price'];
        $search_diliver[] = $row['diliver'];
    }
}



//指定餐,不指定日
else if($admin_days==0&&$admin_ats!==0){

    //echo "指定餐,不指定日".$search_name_str;

    $search_key = $admin_year.$admin_month;
    switch($admin_ats){
        case 1:
            $admin_ats_str="早餐";
            break;
        case 2:
            $admin_ats_str="午餐";
            break;
        case 3:
            $admin_ats_str="晚餐";
            break;
    }

    if($admin_dapartment=='all'){
        $reStr = $admin_year."年".$admin_month."月 ".$admin_ats_str;
        $query = mysql_query("select count(nid) from $tableOrder where (search_key='$search_key' and order_at='$admin_ats')".$search_name_str);
        $result = mysql_query("select * from $tableOrder where         (search_key='$search_key' and order_at='$admin_ats')"
            .$search_name_str." order by nid");
    }


    else{
        $reStr = $admin_year."年".$admin_month."月 ".$admin_ats_str." ".$admin_dapartment;

        $query = mysql_query("select count(nid) from $tableOrder where (search_key='$search_key' and order_at='$admin_ats' and user_department='$admin_dapartment')".$search_name_str);

        $result = mysql_query("select * from $tableOrder where (search_key='$search_key' and order_at='$admin_ats' and user_department='$admin_dapartment'".$search_name_str.") order by nid");
    }
    $sum = mysql_result($query, 0);
    while ($row = mysql_fetch_array($result)) {
        $search_name[] = $row['product_name'];
        $search_id[] = $row['product_id'];
        $search_date[] = $row['order_date'];
        $search_num[] = $row['product_num'];
        $search_at[] = $row['order_at'];
        $search_user[] = $row['user_id'];
        $search_user_name[] = $row['user_name'];
        $search_one_price[] = $row['product_price'];
        $search_price[] = $row['order_price'];
        $search_diliver[] = $row['diliver'];
    }
}

//指定餐,指定日
else if($admin_days!==0&&$admin_ats!==0){
    //echo "指定餐 指定日";
    switch($admin_ats){
        case 1:
            $admin_ats_str="早餐";
            break;
        case 2:
            $admin_ats_str="午餐";
            break;
        case 3:
            $admin_ats_str="晚餐";
            break;
    }

    $searchStr = $admin_year."年".$admin_month."月".$admin_days."日";

    if($admin_dapartment=='all'){
        $reStr = $admin_year."年".$admin_month."月".$admin_days."日 ".$admin_ats_str;
        $query = mysql_query("select count(nid) from $tableOrder where (order_date='$searchStr' and order_at='$admin_ats')".$search_name_str);
        $result = mysql_query("select * from $tableOrder where (order_date='$searchStr' and order_at='$admin_ats')"
            .$search_name_str." order by nid");
    }else{
        $reStr = $admin_year."年".$admin_month."月".$admin_days."日 ".$admin_ats_str." ".$admin_dapartment;
        $query = mysql_query("select count(nid) from $tableOrder where (order_date='$searchStr' and order_at='$admin_ats' and user_department='$admin_dapartment')".$search_name_str);
        $result = mysql_query("select * from $tableOrder where (order_date='$searchStr' and order_at='$admin_ats' and
user_department='$admin_dapartment')".$search_name_str." order by nid");
    }
    $sum = mysql_result($query, 0);
    while ($row = mysql_fetch_array($result)) {
        $search_name[] = $row['product_name'];
        $search_id[] = $row['product_id'];
        $search_date[] = $row['order_date'];
        $search_num[] = $row['product_num'];
        $search_at[] = $row['order_at'];
        $search_user[] = $row['user_id'];
        $search_user_name[] = $row['user_name'];
        $search_one_price[] = $row['product_price'];
        $search_price[] = $row['order_price'];
        $search_diliver[] = $row['diliver'];
    }
}







//传递数组，会员号，日期，餐类，来统计此餐类份数。
function select_count_meal($uid,$date,$meal)
{
    $tableOrder = "orderlist";
    $result = mysql_query("select * from $tableOrder where (user_id='$uid' and order_date='$date' and order_at='$meal')");


    while ($row = mysql_fetch_array($result)) {
        $search_id[] = $row['nid'];
    }

    return count($search_id);
}





//传递数组，会员号，日期，餐类，来统计此餐类份数的价钱。
function select_count_meal_price($uid,$date,$meal)
{
    $tableOrder = "orderlist";
    $result = mysql_query("select * from $tableOrder where (user_id='$uid' and order_date='$date' and order_at='$meal')");


    while ($row = mysql_fetch_array($result)) {
        $search_product_price[] = $row['product_price'];
    }


    for($i=0;$i<=count($search_product_price);$i++)
    {
        $sum_price=$search_product_price[$i]+$sum_price;
    }



    return $sum_price;
}




?>